package com.gingko.duominuo.network.download

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import com.gingko.duominuo.R

/**
 * 描述：
 * -下载Notification类，既可用系统默认的通知布局，也可以用自定义的布局
 * 创建人：baoshengxiang
 * 创建时间：2017/10/25
 */
class DownloadNotificationManager(private val mContext: Context, private val notificationID: Int) {
    private val manager: NotificationManager by lazy {
        mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }
    private var notifyBuilder: NotificationCompat.Builder? = null
    private val channelId = "channel_001"
    private val channelName = "name"

    /**
     * 显示系统默认格式通知
     *
     * @param iconId     通知栏图标ID
     * @param titleText  通知栏标题
     * @param contentStr 通知栏内容
     */
    private fun showDefaultNotification(contentStr: String?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val mChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
            manager.createNotificationChannel(mChannel)
        }
        notifyBuilder = NotificationCompat.Builder(mContext, channelId) /*设置small icon*/
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("开始下载...") /*设置title*/
            .setContentTitle(mContext.getString(R.string.app_name)) /*设为true,notification将无法通过左右滑动的方式清除*/
            .setOngoing(false)
            .setOnlyAlertOnce(true) /*设置进度*/
            .setProgress(100, 0, false) /*设置详细文本*/
            .setContentText(contentStr)
        val notification = notifyBuilder?.build()
        manager.notify(notificationID, notification)
    }

    /**
     * 更改自定义布局文件中的进度条的值
     *
     * @param progress 进度值(0~100)
     */
    fun changeProgressStatus(state: State, progress: Int = 0) {
        when (state) {
            State.DOWNLOAD_START -> {
                showDefaultNotification("开始下载")
            }
            State.DOWNLOAD_FAIL -> {
                notifyBuilder?.setContentText("下载错误，稍后重试！")
                notifyBuilder?.setOngoing(false)
                notifyBuilder?.setAutoCancel(true)
            }
            State.DOWNLOAD_COMPLETE -> {
                notifyBuilder?.setProgress(100, 100, false)
                notifyBuilder?.setContentText("下载完成，开始安装")
                notifyBuilder?.setOngoing(false)
                notifyBuilder?.setAutoCancel(true)
                removeNotification()
            }
            State.DOWNLOAD_CANCEL -> {
                removeNotification()
            }
            State.DOWNLOADING -> {
                notifyBuilder?.setProgress(100, progress, false)
                notifyBuilder?.setContentText("当前进度($progress%)")
                notifyBuilder?.setOngoing(true)
                notifyBuilder?.setAutoCancel(false)
            }
            State.DOWNLOAD_PAUSE -> {
                notifyBuilder?.setContentText("下载暂停")
                notifyBuilder?.setOngoing(false)
                notifyBuilder?.setAutoCancel(true)
            }
        }
        notifyBuilder?.let {
            manager.notify(notificationID, it.build())
        }
    }

    /**
     * 移除通知
     */
    private fun removeNotification() {
        // 取消的只是当前Context的Notification
        manager.cancel(notificationID)
    }

    enum class State {
        DOWNLOAD_START,
        DOWNLOAD_CANCEL,
        DOWNLOAD_PAUSE,
        DOWNLOAD_COMPLETE,
        DOWNLOAD_FAIL,
        DOWNLOADING,
    }

}